package Product

import (
	"OnlineRetailers/controllers/errorDeal"
	"OnlineRetailers/models/Admin"
	"OnlineRetailers/models/User/resMes"
	error2 "OnlineRetailers/models/errorDeal"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
)

type GetProductByActivity struct {
	beego.Controller
}

func (c *GetProductByActivity) GetProductByActivity() {
	defer func() {
		c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
		c.Ctx.Output.Status = 200
		c.ServeJSON()
	}()
	var addr = "/Product/GetProductByActivity"
	res := resMes.GetActivity{}
	activity := &Admin.Activity{}
	log := &error2.Log{}
	var err error
	o := orm.NewOrm()

	activityId := c.GetString("activityId")
	begin, err := c.GetInt("begin", 0)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}
	filter, err := c.GetInt("filter", 0)
	if err != nil {
		c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
		return
	}

	sql := "select * from `activity` where activity_id=?"
	_, err = o.Raw(sql, activityId).QueryRows(&activity)
	for i := 0; i < len(activityId); i++ {
		err = GetProductByClassId(activity.Classification[i].ClassificationId, activity.Classification[i], begin, filter)
		if err != nil {
			c.Data["json"] = errorDeal.ErrMess(log, "400", err, addr, err.Error())
			return
		}
	}

	res.Activity = activity
	res.Code = "200"
	c.Data["json"] = res

}
